
f_in = open('input31', 'r')
f_out = open('output.txt', 'w')

T = int(f_in.readline().rstrip())

for t in range(1,T+1):
    (N, P1, W1, M, K, A, B, C, D) = [int(x) for x in f_in.readline().rstrip().split()]
    
    for i in range(1,N):
        p = ((A*P1 + B) % M) + 1
        w = ((C*W1 + D) % K) + 1
        P1 = p
        W1 = w
        
    print(N, P1, W1, M, K, A, B, C, D)
      
    
f_in.close()
f_out.close()